package com.tz.controller;

import com.tz.entity.RightControl;
import com.tz.entity.Role;
import com.tz.entity.User;
import com.tz.entity.json.PassWord;
import com.tz.entity.json.UserJson;
import com.tz.entity.json.UserRights;
import com.tz.entity.search.UserSearchPojo;
import com.tz.service.RoleService;
import com.tz.service.UserService;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Alex
 * @date 18-1-3
 */
@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserController {

    private final UserService userService;
    private final RoleService roleService;


    @Autowired
    public UserController(RoleService roleService, UserService userService) {
        this.roleService = roleService;
        this.userService = userService;
    }


    @RequestMapping("/index")
    public String index(Model model, @SessionAttribute(value = "user", required = false) User user) {
        if (user == null) {
            return "redirect:/login/prelogin";
        } else {

            List<RightControl> myRights = roleService.getRole(user.getRole().getRoleid());

            Map<String, List<RightControl>> map = new HashMap<>(16);


            for (RightControl rc : myRights) {
                String title = rc.getCtrname().split(":")[0];
                rc.setCtrname(rc.getCtrname().split(":")[1]);
                if (map.containsKey(title)) {
                    map.get(title).add(rc);
                } else {
                    List<RightControl> list = new ArrayList<>();
                    list.add(rc);
                    map.put(title, list);
                }
            }

            List<UserRights> rights = new ArrayList<>();

            for (String key : map.keySet()) {
                UserRights ur = new UserRights();
                ur.setTitle(key);
                ur.setRights(map.get(key));
                rights.add(ur);
            }
            model.addAttribute("rights", rights);

            return "index";
        }

    }

    @ResponseBody
    @RequestMapping("/list")
    public PageBean<User> list(@RequestParam("pageNow") Integer pageNow, @RequestParam("pageSize") Integer pageSize, UserSearchPojo userSearchPojo) {
        System.out.println(userSearchPojo);
        PageBean<User> users = userService.queryUserByPage(pageNow, pageSize, userSearchPojo);
        return users;
    }

    @ResponseBody
    @RequestMapping("/role")
    public List<Role> roleList() {
        return roleService.getAllRoles();
    }

    @ResponseBody
    @RequestMapping("/change")
    public String change(@RequestBody UserJson userJson) {

        Role role = new Role();
        role.setRoleid(userJson.getRoleid());
        userJson.setRole(role);
        System.out.println(userJson);
        if (userJson.getUserid() == null) {
            userJson.setHeadurl("default.jpg");
            userJson.setPassword("123456");
            userService.addUser(userJson);
        } else {
            userService.changeUser(userJson);
        }
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/enable")
    public String enableUser(@RequestBody String str) {
        String[] strings = str.split(":");

        List<Long> ids = new ArrayList<>();
        String[] idStr = strings[1].split(",");
        for (String id : idStr) {

            ids.add(Long.parseLong(id));
        }
        System.out.println(ids);
        if (strings[0].equals("1")) {
            userService.changeBatchUserStatus(ids, 1);
        } else if (strings[0].equals("2")) {
            userService.resetPassword(ids);
        } else if (strings[0].equals("0")) {
            userService.changeBatchUserStatus(ids, 0);
        }
        return "ok";
    }


    @ResponseBody
    @RequestMapping("/password")
    public String modPassword(@RequestBody PassWord passWord, @SessionAttribute("user") User user) {
        if(passWord!=null&&passWord.getNewpwd0().equals(passWord.getNewpwd1())){
            System.out.println(passWord);
            if (userService.login(user.getUsername(), passWord.getOldpwd()) != null) {

                user.setPassword(passWord.getNewpwd0());
                userService.changeUser(user);
                return "ok";

            } else {
                return "false";
            }
        }else {
            return "new";
        }




    }


}
